package com.example.testcomposeproject.pages.testAndroidView

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.testcomposeproject.PreViewData
import com.example.testcomposeproject.pages.copy.CopyViewModel
import com.example.testcomposeproject.ui.theme.AppTheme

/**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc: 这个思路可以包装高德地图的MapView
 *
 */
@Preview(showBackground = true, device = PreViewData.PreviewPhoneSize)
@Composable
fun PreViewTestAndroidViewUI(modifier: Modifier = Modifier) {
    AppTheme {
        TestAndroidViewUI(modifier = modifier)

    }
}

/**
 * viewModel 用这个方法
 */
@Composable
fun TestAndroidViewScreen(modifier: Modifier = Modifier,viewModel: CopyViewModel = hiltViewModel()) {
    TestAndroidViewUI(modifier)
}

/**
 * 真正的UI元素
 */
@Composable
fun TestAndroidViewUI(modifier: Modifier = Modifier) {
    CustomViewContainer(modifier = modifier)
}


@Composable
fun CustomViewContainer(modifier: Modifier = Modifier) {
    val context = LocalContext.current

    // 使用 AndroidView 将自定义 View 集成到 Compose 中
    AndroidView(
        factory = {
            CustomView(context).apply {
                // 可以在这里进行自定义 View 的初始化配置
                paint.color= android.graphics.Color.BLUE
            }
        },
        modifier = modifier
    )
}